﻿using System;
using System.Web.Security;
using System.Web;
using FFIV.Lib.BusinessRules;

namespace FFIV.Lib.Utilities
{
	public class UsersUtility
	{
		/// <summary>
		/// 修改显示姓名
		/// </summary>
		/// <param name="UserId"></param>
		/// <param name="ShowName"></param>
		public static void ShowNameReset(Guid? UserId, string ShowName)
		{
			MembershipUser user = Membership.GetUser(UserId);
			user.Comment = ShowName;
			Membership.UpdateUser(user);
			HttpContext.Current.Session["UserName"] = user.Comment;
			HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <param name="UserId"></param>
		/// <param name="NewPassword"></param>
		public static void PasswordReset(Guid? UserId, string NewPassword)
		{
			MembershipUser user = Membership.GetUser(UserId);
			string temp_Password = user.ResetPassword();
			user.ChangePassword(temp_Password, NewPassword);
		}
		/// <summary>
		/// 验证并登陆系统
		/// </summary>
		public static bool LogIn(string userName, string userPassword)
		{
			if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userPassword))
			{
				return false;
			}

			MembershipUser user = Membership.GetUser(userName);
			if (user != null && Membership.ValidateUser(userName, userPassword))
			{
				HttpContext.Current.Session.Add("Email", user.UserName);
				HttpContext.Current.Session.Add("UserName", user.Comment);
				HttpContext.Current.Session.Add("UserId", user.ProviderUserKey.ToString());

				string role = UserRules.GetUserRoleByUserId(new Guid(user.ProviderUserKey.ToString()));
				HttpContext.Current.Session.Add("Role", role);

				return true;
			}
			return false;
		}

		/// <summary>
		/// 注销当前用户
		/// </summary>
		public static void LogOut()
		{
			string email = GetCurrentUserEmail();
			if (email != null)
			{
				MembershipUser user = Membership.GetUser(email);
				if (user != null)
				{
					HttpContext.Current.Session.Remove("Email");
					HttpContext.Current.Session.Remove("UserName");
					HttpContext.Current.Session.Remove("UserId");
					HttpContext.Current.Session.Remove("Role");
				}
			}
		}

		/// <summary>
		/// 获得用户的Guid类型的UserId，
		/// </summary>
		/// <returns></returns>
		public static Guid? GetCurrentUserId()
		{
			if (HttpContext.Current.Session["UserId"] != null)
			{
				Guid UserId = new Guid(HttpContext.Current.Session["UserId"].ToString());
				return UserId;
			}
			return null;
		}

		/// <summary>
		/// 获得用户的显示姓名Comment
		/// </summary>
		/// <returns></returns>
		public static string GetCurrentUserName()
		{
			if (HttpContext.Current.Session["UserName"] != null)
			{
				return HttpContext.Current.Session["UserName"].ToString();
			}
			return null;
		}

		/// <summary>
		/// 获得用户注册用户名Email
		/// </summary>
		/// <returns></returns>
		public static string GetCurrentUserEmail()
		{
			if (HttpContext.Current.Session["Email"] != null)
			{
				return HttpContext.Current.Session["Email"].ToString();
			}
			return null;
		}

		/// <summary>
		/// 获取用户当前角色
		/// </summary>
		public static Permissions GetCurrentUserRole()
		{
			if (HttpContext.Current.Session["Role"] != null)
			{
				string role = HttpContext.Current.Session["Role"].ToString().ToLower();
				switch (role)
				{
					case "admin":
						return Permissions.Admin;
					case "author":
						return Permissions.Author;
					default:
						return Permissions.Anonymous;
				}
			}
			else
			{
				return Permissions.Anonymous;
			}
		}

		public static bool IsAdmin()
		{
			return GetCurrentUserRole() == Permissions.Admin;
		}

		public static bool IsAuchor()
		{
			return GetCurrentUserRole() == Permissions.Author;
		}

		public static bool IsAnomymous()
		{
			return GetCurrentUserRole() == Permissions.Anonymous;
		}
	}
}
